package cn.iocoder.yudao.module.member.controller.admin.usergastemplaterelation;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.member.controller.admin.usergastemplaterelation.vo.*;
import cn.iocoder.yudao.module.member.dal.dataobject.usergastemplaterelation.UserGasTemplateRelationDO;
import cn.iocoder.yudao.module.member.service.usergastemplaterelation.UserGasTemplateRelationService;

@Tag(name = "管理后台 - 燃气户与安检模板关系")
@RestController
@RequestMapping("/member/user-gas-template-relation")
@Validated
public class UserGasTemplateRelationController {

    @Resource
    private UserGasTemplateRelationService userGasTemplateRelationService;

    @PostMapping("/create")
    @Operation(summary = "创建燃气户与安检模板关系")
    @PreAuthorize("@ss.hasPermission('member:user-gas-template-relation:create')")
    public CommonResult<Long> createUserGasTemplateRelation(@Valid @RequestBody UserGasTemplateRelationSaveReqVO createReqVO) {
        return success(userGasTemplateRelationService.createUserGasTemplateRelation(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新燃气户与安检模板关系")
    @PreAuthorize("@ss.hasPermission('member:user-gas-template-relation:update')")
    public CommonResult<Boolean> updateUserGasTemplateRelation(@Valid @RequestBody UserGasTemplateRelationSaveReqVO updateReqVO) {
        userGasTemplateRelationService.updateUserGasTemplateRelation(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除燃气户与安检模板关系")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('member:user-gas-template-relation:delete')")
    public CommonResult<Boolean> deleteUserGasTemplateRelation(@RequestParam("id") Long id) {
        userGasTemplateRelationService.deleteUserGasTemplateRelation(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得燃气户与安检模板关系")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('member:user-gas-template-relation:query')")
    public CommonResult<UserGasTemplateRelationRespVO> getUserGasTemplateRelation(@RequestParam("id") Long id) {
        UserGasTemplateRelationDO userGasTemplateRelation = userGasTemplateRelationService.getUserGasTemplateRelation(id);
        return success(BeanUtils.toBean(userGasTemplateRelation, UserGasTemplateRelationRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得燃气户与安检模板关系分页")
    @PreAuthorize("@ss.hasPermission('member:user-gas-template-relation:query')")
    public CommonResult<PageResult<UserGasTemplateRelationRespVO>> getUserGasTemplateRelationPage(@Valid UserGasTemplateRelationPageReqVO pageReqVO) {
        PageResult<UserGasTemplateRelationDO> pageResult = userGasTemplateRelationService.getUserGasTemplateRelationPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, UserGasTemplateRelationRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出燃气户与安检模板关系 Excel")
    @PreAuthorize("@ss.hasPermission('member:user-gas-template-relation:export')")
    @OperateLog(type = EXPORT)
    public void exportUserGasTemplateRelationExcel(@Valid UserGasTemplateRelationPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<UserGasTemplateRelationDO> list = userGasTemplateRelationService.getUserGasTemplateRelationPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "燃气户与安检模板关系.xls", "数据", UserGasTemplateRelationRespVO.class,
                        BeanUtils.toBean(list, UserGasTemplateRelationRespVO.class));
    }

}